package com.b2c.ftysys.sevice.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.create.NewCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.b2c.ftysys.dao.B2cFsyPropriceDAO;
import com.b2c.ftysys.domain.B2cFsyProprice;
import com.b2c.ftysys.domain.B2cFsyPropriceExample;
import com.b2c.ftysys.domain.B2cFsyWs;
import com.b2c.ftysys.domain.Page;
import com.b2c.ftysys.sevice.ProPriceService;
import com.b2c.ftysys.sevice.WsInfoService;
import com.b2c.ftysys.view.application.AppErrorException;
import com.b2c.ftysys.view.application.fileImport.FileImport;

@Service
@RemoteProxy(creator=NewCreator.class,name="proPriceService")
public class ProPriceServiceImpl implements ProPriceService {
	@Autowired
	B2cFsyPropriceDAO priceDao;
	@Autowired
	WsInfoService wsService;
	
	@Resource(name="orderFileImportor")
	private FileImport convert;

	public WsInfoService getWsService() {
		return wsService;
	}

	public void setWsService(WsInfoService wsService) {
		this.wsService = wsService;
	}

	public B2cFsyPropriceDAO getPriceDao() {
		return priceDao;
	}

	public void setPriceDao(B2cFsyPropriceDAO priceDao) {
		this.priceDao = priceDao;
	}

	public int ImportExcel(String fileAddr) throws AppErrorException {
		List<Map<String, Object>> data = convert.importData(fileAddr);

		// ���
		// �������(����)

		B2cFsyProprice proPrice = null;
		int i = 0;
		for (Map<String, Object> dataMap : data) {

			for (B2cFsyWs ws : wsService.queryAllWs()) {
				String style = String.valueOf(dataMap.get("���"));
				if (style.indexOf(".") > 0) {
					style = style.substring(0, style.indexOf("."));
				}
				if (dataMap.get(ws.getWsName()) != null) {
					proPrice = new B2cFsyProprice();
					proPrice.setProStyleNo(style);

					proPrice.setWsId(ws.getWsId());
					if (dataMap.get(ws.getWsName()) instanceof Double) {
						Double price = (Double) dataMap.get(ws.getWsName());
						proPrice.setPrice(new BigDecimal(price));
					} else {
						try {
							HSSFRichTextString price = (HSSFRichTextString) dataMap
									.get(ws.getWsName());
							proPrice.setPrice(new BigDecimal(price.getString()));
						} catch (Exception e) {
							continue;
						}
					}

					B2cFsyPropriceExample example = new B2cFsyPropriceExample();
					example.createCriteria().andProStyleNoEqualTo(style)
							.andWsIdEqualTo(ws.getWsId());

					if (priceDao.countByExample(example) > 0) {
						continue;
					} else
						priceDao.save(proPrice);

				} else
					continue;
			}
			i++;
		}
		return i;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryAllStylePrice(int startPageNo,
			int pageSize) {
		Page page = new Page();
		B2cFsyPropriceExample example = new B2cFsyPropriceExample();
		page.setMessage_count(priceDao.countByExample(example));
		page.setPage_size(pageSize);
		page.setNowPage(startPageNo);

		try {
			return priceDao.getSqlClient().queryForList(
					"b2c_fsy_proprice.abatorgenerated_selectAll",
					page.getStartOffset(), pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Float getStylePrice(Integer wsId, String styleNo) {
		if (styleNo == null || styleNo.trim().length() <= 0)
			return new Float(0);
		else if (wsId == null || wsId == 0) {
			return new Float(0);
		}

		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("wsId", wsId);
		dataMap.put("proStyleNo", styleNo);
		Float price = new Float(0);
		for (Map<String, Object> data : queryStylePriceListByClause(wsId,
				styleNo)) {
			try {
				price = Float.parseFloat(String
						.valueOf(data.get("price")));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return price;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryStylePriceListByClause(Integer wsId,
			String styleNo) {

		if (styleNo != null && styleNo.trim().length() <= 0)
			styleNo = null;

		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("wsId", wsId);
		dataMap.put("proStyleNo", styleNo);

		try {
			return priceDao.getSqlClient().queryForList(
					"b2c_fsy_proprice.abatorgenerated_selectByExample_Map",
					dataMap);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public int regStylePrice(B2cFsyProprice param) {
		B2cFsyPropriceExample example = new B2cFsyPropriceExample();
		example.createCriteria().andProStyleNoEqualTo(param.getProStyleNo())
				.andWsIdEqualTo(param.getWsId());

		if (priceDao.countByExample(example) > 0)
			return 0;
		else
			priceDao.save(param);

		return 1;
	}

	public int removeStylePrice(int paramId) {
		return priceDao.deleteByPrimaryKey(paramId);
	}

	public int updateStylePrice(B2cFsyProprice param) {
		return priceDao.updateByPrimaryKeySelective(param);
	}

}
